/**
 * MessagesGenerator.java
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */
package unibo.lsb.graphic;

import unibo.lsb.logic.CoveredDice;
import unibo.lsb.logic.Dice;
import unibo.lsb.logic.LastAssignedPenalty;
import unibo.lsb.logic.Player;
import unibo.lsb.logic.PlayerId;

/**
 * Utility class that generates standard messages to be displayed during the
 * game
 */
public class MessagesGenerator {

	/**
	 * Notifies the subject of the phrase is the current player, "You"
	 */
	public static final int PLAYER_SELF = 0;
	/**
	 * Notifies the subject of the phrase is another player
	 */
	public static final int PLAYER_OTHER = 1;

	/**
	 * Same as
	 * {@link MessagesGenerator#playerDeclaresDiceMessage(PlayerId, Dice)} but
	 * takes a {@link Player} instead of a {@link PlayerId}
	 * 
	 * @param p
	 *            the player subject of the phrase
	 * @param d
	 *            the dice cast
	 * @return a message like "A cast 52"
	 */
	public static String playerDeclaresDiceMessage(PlayerId p, Dice d) {
		return p.getName() + " declares " + d.toString();
	}

	public static String youCastDiceAndDeclare(CoveredDice cd) {
		return "You cast " + cd.getCovered().toString() + " and declare "
				+ cd.getDeclared().toString();
	}

	/**
	 * Same as {@link MessagesGenerator#playerDeclaresDiceMessage(Player, Dice)}
	 * but takes a {@link PlayerId} instead of a {@link Player}
	 * 
	 * @param p
	 *            the player subject of the phrase
	 * @param d
	 *            the dice cast
	 * @return a message like "A cast 52"
	 */
	public static String playerDeclaresDiceMessageToPlayer(PlayerId sender,
			CoveredDice cd, PlayerId receiver) {
		return sender.getName() + " passed " + cd.getDeclared().toString()
				+ " to " + receiver.getName();
	}

	public static String playerDeclaresDiceToYou(CoveredDice cd) {
		return cd.getPlayerId().getName() + " passed you "
				+ cd.getDeclared().toString();
	}

	public static String youCastAndDeclareDiceToPlayer(CoveredDice cd,
			PlayerId receiver) {
		return "You cast " + cd.getCovered().toString() + " and declare "
				+ cd.getDeclared().toString() + " to " + receiver.getName();
	}

	/**
	 * 
	 * @param playerType
	 *            can be {@link MessagesGenerator#PLAYER_SELF} or
	 *            {@link #PLAYER_OTHER} to indicate the subject of the phrase is
	 *            "you" or a proper name
	 * @param cd
	 *            the
	 * @return message like "You declared 65 but made 61" or
	 *         "Keith declared 44 but made 43"
	 */
	public static String xDeclaredDiceButMadeOtherDice(int playerType,
			CoveredDice cd) {
		String msg = "";
		if (playerType == PLAYER_SELF) {
			msg = "You ";
		} else {
			msg = cd.getPlayerId().getName();
		}
		msg = msg + " declared " + cd.getDeclared().toString();

		if (cd.getDeclared().toString().equals(cd.getCovered().toString())) {
			msg = msg + " and";
		} else {
			msg = msg + " but";
		}
		msg = msg + " made " + cd.getCovered().toString();
		return msg;
	}

	/**
	 * @param playerType
	 *            set {@link #PLAYER_SELF} to say "You", {@link #PLAYER_OTHER}
	 *            instead.
	 * @param lap
	 * @return messages like "Silvia gets 1 point penalty!" or
	 *         "You get 1 point penalty!"
	 */
	public static String penaltyForDoubting(int playerType,
			LastAssignedPenalty lap) {
		String msg;
		int p = lap.getPenalty();

		if (playerType == PLAYER_SELF) {
			msg = "You get ";
		} else {
			msg = lap.getPlayer().getName() + " gets ";
		}
		msg = msg + p;
		if (p == 1) {
			msg = msg + " point ";
		} else {
			msg = msg + " points ";
		}
		msg = msg + "penalty.";
		return msg;
	}

	/**
	 * @param penalty
	 *            value of the penalty
	 * @return messages like
	 *         "You were doubted! You get 1 point penalty for lying."
	 */
	public static String youGetPenalty(int penalty) {
		String msg = "You were doubted! You get " + penalty;
		if (penalty == 1) {
			msg = msg + " point";
		} else {
			msg = msg + " points";
		}
		msg = msg + " penalty for lying";
		return msg;
	}

	public static String aReceivedDiceFormB(int playerType, String senderName,
			CoveredDice cd) {
		String msg = "";
		if ((playerType == PLAYER_SELF) && !(senderName.equals(""))) {
			throw new IllegalArgumentException(
					"If it's PLAYER_SELF, you shall not set senderName");
		} else if ((playerType == PLAYER_OTHER) && (senderName.equals(""))) {
			throw new IllegalArgumentException(
					"If it's PLAYER_OTHER, you must set senderName");
		} else if (playerType == PLAYER_SELF) {
			msg = msg + "I";
		} else {
			msg = msg + senderName;
		}
		msg = msg + " received dice from " + cd.getPlayerId().getName() + ".";
		return msg;
	}

	public static String outOfTheGame() {
		return "You've exceeded penalties, you're out of the game";
	}

	public static String outOfTheGame(PlayerId p) {
		return p.getName() + " exceeded penalties and is out of the game";
	}

	// public static String myTurn() {
	// return "It's your turn";
	// }
	//
	// public static String turn(String name) {
	// return "It's " + name + "'s turn";
	// }

	public static String acceptAndReCast(PlayerId p, Dice declared) {
		return p.getName() + " accepted the " + declared.toString()
				+ " and cast again";
	}

	public static String doubt(PlayerId p, Dice declared) {
		return p.getName() + " doubted the " + declared.toString();
	}

	public static String doubt(Dice declared) {
		return "You doubt the " + declared.toString();
	}

	public static String accepted21() {
		return "You get 1 point penalty for accepting 21";
	}

	public static String accepted21(String name) {
		return name + " gets 1 point penalty for accepting 21";
	}

	public static String everyoneLost() {
		return "Game over! Everyone lost.";
	}

	public static String gameOver() {
		return "Game over";
	}

	public static String youWin() {
		return "Game over! You WIN!";
	}

	public static String winner(String name) {
		return "Game over! " + name + " is the winner!";
	}

	/**
	 * @param coveredDice
	 * @return
	 */
	public static String youAccept(CoveredDice coveredDice) {
		return "You accept the " + coveredDice.getDeclared().toString();
	}
}
